package Backend

import (
	"fmt"
	"github.com/syyongx/php2go"
	"hello/controllers/Base"
	"hello/models"
	"hello/wl"
	"strconv"
)

type PostController struct {
	Base.BaseController

}

// @router / [get]
//所有文章
func (this *PostController) Posts() {

	this.PageInit()

	var postUser models.PostUser

	postUserList,count:=postUser.PostSelect(this.Limit,this.Page)

	this.AjaxSuccess(map[string]interface{}{"post":postUserList,"count":count})

}


//创建文章
func (this *PostController) PostAdd() {
	context:=this.GetPost("context")
	title:=this.GetPost("title")

	user:=this.GetUser()
	post:=models.Post{UserId:user.Id,Content:context.(string),Title:title.(string)}
	post.CreatePost()

	if php2go.Empty(post.Id) {
		this.AjaxError("添加失败!")
	}else{
		this.AjaxSuccess(post)
	}
}


//删除文章
func (this *PostController) PostDel() {
	id,_:=this.GetInt(":id")

	if php2go.Empty(id) {
		this.AjaxError("错误Action!")
	}

	this.GetUser()

	post:=models.Post{Id:id}
	rows:=post.DeletePost()

	if php2go.Empty(rows) {
		this.AjaxError("删除失败!")
	}else{
		this.AjaxSuccess("删除成功!")
	}
}

//编辑文章
func (this *PostController) PostEditAction() {
	id:=this.GetPost("id")
	context:=this.GetPost("context")
	title:=this.GetPost("title")

	if php2go.Empty(id) {
		this.AjaxError("错误Action!")
	}

	user:=this.GetUser()
	post:=models.Post{Id:wl.ToInt(id.(string)),UserId:user.Id,Content:context.(string),Title:title.(string)}

	fmt.Println(post)

	rows:=post.UpdatePost()

	fmt.Println(rows)

	if php2go.Empty(rows) {
		this.AjaxError("修改失败!")
	}else{
		this.AjaxSuccess("修改成功!")
	}
}



//文章详情页
func (this *PostController) PostShow() {
	id, _ :=this.GetInt(":id")
	if php2go.Empty(id) {
		this.AjaxError("错误参数!")
	}

	var post models.PostShow
	var comments []models.Comments

	//文章内容
	comments=post.PostFind(id)

	fmt.Println(post)

	this.AjaxSuccess(map[string]interface{}{"post":post,"comments":comments})
}

//是否赞
func (this *PostController) IsZanAction() {

	postId, _ :=this.GetInt(":post_id")
	if php2go.Empty(postId) {
		this.AjaxError("错误参数!")
	}
	var user =this.GetUser()

	zan:=models.Zan{UserId:user.Id,PostId:postId}

	zan.IsZan()

	if !php2go.Empty(zan.Id){
		this.AjaxSuccess(true)
	}else{
		this.AjaxError(false)
	}

}

//点赞+取消点赞
func (this *PostController) ZanAction() {

	id, _ :=this.GetInt(":id")
	if php2go.Empty(id) {
		this.AjaxError("错误参数!")
	}
	cacheUser:=this.GetUser()
	fmt.Println("登陆的用户为",cacheUser)

	//var zanFind models.Zan
	zan:=models.Zan{UserId:cacheUser.Id,PostId:id}

	fmt.Println("刚开始的结果",zan)

	zan.IsZan()

	fmt.Println("查询的结果",zan)

	if php2go.Empty(zan.Id) {//没有就赞
		zan.CreatedZan()
		this.AjaxSuccess(true)
	}else{
		zan.DeleteZan()
		this.AjaxSuccess(false)
	}

}




//评论
func (this *PostController) CommentCreateAction() {

	id :=this.GetPost("id")
	context:=this.GetPost("context")

	if php2go.Empty(id) || php2go.Empty(context) {
		this.AjaxError("错误参数!")
	}

	cacheUser:=this.GetUser()

	idInt,_:=strconv.Atoi(id.(string))

	comment:=models.CommentInster{UserId:cacheUser.Id,PostId:idInt,Content:context.(string)}

	comment.CreateCommit()

	this.AjaxSuccess(comment)

}
